package com.library.borrow.service;

import com.framework.Page;
import com.library.borrow.dao.BorrowDaoImpl;
import com.library.entity.Book;
import com.library.entity.Borrow;
import com.library.entity.Staff;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly=false)
public class BorrowServiceImpl {
	
	@Resource
	private BorrowDaoImpl borrowDaoImpl;

	public void addBorrow(Borrow borrow){
		this.borrowDaoImpl.saveBorrow(borrow);
	}

    @Transactional(readOnly=true)
	public Borrow getBorrow(Integer borrowId){
		return this.borrowDaoImpl.getBorrow(borrowId);
	}

	@Transactional(readOnly=true)
	public List<Borrow> getBorrows(Map paramsMap){
		return this.borrowDaoImpl.findBorrowsByProperty(paramsMap);
	}

	@Transactional(readOnly=true)
	public Page<Borrow> getBorrowPage(int pageNum,int pageSize,Map paramsMap){
		return this.borrowDaoImpl.findPageByProperty(pageNum,pageSize,paramsMap);
	}

    @Transactional(readOnly=true)
	/*查找全部借阅信息*/
	public List<Borrow> getAllBorrows(){
		return this.getBorrows(new HashMap());
	}

	/*查找全部借阅信息 分页*/
	public Page<Borrow> getAllBorrows(int pageNum,int pageSize,Map paramsMap){
		return this.getBorrowPage(pageNum,pageSize,paramsMap);
	}

	/*查找该员工当前借阅的借阅单，包括未取书，借阅中，归还中，挂失，已超期的 分页*/
	public Page<Borrow> getCurrentBorrowsByStaff(int pageNum,int pageSize,Staff staff){
		return this.borrowDaoImpl.findCurrentBorrowsByStaff(pageNum,pageSize,staff);
	}

	public List<Borrow> getCurrentBorrowsByStaff(Staff staff){
		return this.borrowDaoImpl.findCurrentBorrowsByStaff(staff);
	}

	/*查找该员工已经归还的借阅单*/
	public Page<Borrow> getHistoryBorrowsByStaff(int pageNum,int pageSize,Staff staff){
		return this.borrowDaoImpl.findHistoryBorrowsByStaff(pageNum,pageSize,staff);
	}
	public List<Borrow> getHistoryBorrowsByStaff(Staff staff){
		return this.borrowDaoImpl.findHistoryBorrowsByStaff(staff);
	}

	/*查找该员工当前借阅的书籍，包括未取书，借阅中，归还中，挂失，已超期的*/
	public List<Book> getCurrentBooksByStaff(Staff staff){
		List<Borrow> borrows = this.borrowDaoImpl.findCurrentBorrowsByStaff(staff);
		List<Book> books = new ArrayList<>();

		for(int i = 0;i < borrows.size();++i){
			books.add(borrows.get(i).getBook());
		}
		return books;
	}

	@Transactional(readOnly=true)
	public Long getBorrowsCountByParams(Map paramsMap){
		return this.borrowDaoImpl.findCountByProperty(paramsMap);
	}

	/*更新借阅信息*/
	public void updateBorrow(Borrow borrow){
		this.borrowDaoImpl.updateBorrow(borrow);
	}

	/*删除*/
	public void deleteBorrow(int borrowId){
		Borrow bo = this.getBorrow(borrowId);
		this.borrowDaoImpl.deleteBorrow(bo);
    }

	/*删除多个*/
	public void deleteBorrowGroup(int[] borrows){
		Borrow bo = null;
		for(int i = 0;i < borrows.length;++i){
			bo = this.getBorrow(borrows[i]);
			this.borrowDaoImpl.deleteBorrow(bo);
		}
	}

	/*续借*/
	public void continueBorrow(Borrow borrow) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		borrow.setEndDate(sdf.format(sdf.parse(borrow.getEndDate()).getTime() +  24*60*60*1000*10));

		this.updateBorrow(borrow);

	}

}
